﻿
#region Using

using Rio.Framework.Business.Repository;
using System; 
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities; 
using NetBay.Core.BusinessEntities.Connectors;
using NetBay.Core.Tools;

#endregion Using 

namespace Rio.Applications.Web.Services
{
    public partial class BusinessEntitiesDomainService : DomainService
    {
        #region Selection

        /// <summary>
        /// Get RIO's connector name.
        /// </summary>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public List<string> GetConnectorNameList()
        {
            //Variable declaration.
            var l_oConnectorListName = new List<string>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.GetConnectorNameList --> No parameters");

            try
            {                
                l_oConnectorListName = ConnectorRepository.GetConnectorNameList();
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.GetConnectorNameList Exception [" + ex.Message + "]");
            }
            
            //Return value.
            return l_oConnectorListName;
        }

        /// <summary>Get RIO's connector information list.</summary>
        /// <returns>Connector's informations</returns>        
        [Query(HasSideEffects = true)]
        public List<Connector> GetConnectorInformationList()
        {
            //Variable declaration.
            var l_oConnectorInformationList = new List<Connector>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.GetConnectorInformationList --> No parameters");

            try
            {                
                l_oConnectorInformationList = ConnectorRepository.GetConnectorInformationList();
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.GetConnectorInformationList Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oConnectorInformationList;
        }

        /// <summary>Get RIO's folder type index.</summary>
        /// <param name="p_sFolderTypeTableName">Folder type table name.</param> 
        /// <returns>Index correspondence name list.</returns>        
        [Query(HasSideEffects = true)]
        public List<ConnectorCorrespondence> GetFolderTypeIndexCorrespondanceList(string p_sFolderTypeTableName)
        {
            //Variable declaration.
            var l_oFolderTypeIndexCorrespondanceList = new List<ConnectorCorrespondence>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.GetFolderTypeIndexCorrespondanceList (p_sFolderTypeTableName <" + p_sFolderTypeTableName + ">)");

            try
            {                
                l_oFolderTypeIndexCorrespondanceList = ConnectorRepository.GetFolderTypeIndexCorrespondanceList(p_sFolderTypeTableName);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.GetFolderTypeIndexCorrespondanceList Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oFolderTypeIndexCorrespondanceList;
        }

        #endregion Selection

        #region Added

        /// <summary>Add RIO connector.</summary>
        /// <param name="p_sConnectorName">Connector name to add.</param>
        /// <param name="p_sConnectorDescription">Connector description to add.</param>
        /// <returns>Connector Identifiant. (recon_id)</returns>
        [Invoke(HasSideEffects = true)]
        public ReturnLong AddConnector(string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable declaration.
            var l_lConnectorId = new ReturnLong();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.AddConnector (p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");
            
            try
            {
                l_lConnectorId = ConnectorRepository.AddConnector(p_sConnectorName, p_sConnectorDescription);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.AddConnector Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_lConnectorId;
        }

        /// <summary>Add RIO correspondence connector.</summary>
        /// <param name="p_sLstSerializeConnectorCorrespondenceToAdd">Connector Correspondence Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        /// <param name="p_lEntityId">Entity Id.</param>
        /// <param name="p_lFolderTypeId">Folder Type Id.</param>       
        [Invoke(HasSideEffects = true)]
        public void AddCorrespondenceConnector(string p_sLstSerializeConnectorCorrespondenceToAdd, long p_lConnectorId, long p_lEntityId, long p_lFolderTypeId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.AddCorrespondenceConnector (p_lEntityId <" + p_lEntityId + ">, p_lFolderTypeId <" + p_lFolderTypeId + ">, p_lConnectorId <" + p_lConnectorId + ">, p_sLstSerializeConnectorCorrespondenceToAdd <" + p_sLstSerializeConnectorCorrespondenceToAdd + ">)");

            //Deserialize List ConnectorCorrespondence object.
            List<ConnectorCorrespondence> p_oLstConnectorCorrespondence = SerializationHelper.Deserialize<List<ConnectorCorrespondence>>(p_sLstSerializeConnectorCorrespondenceToAdd); 

            try
            {
                ConnectorRepository.AddCorrespondenceConnector(p_oLstConnectorCorrespondence, p_lConnectorId, p_lEntityId, p_lFolderTypeId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.AddCorrespondenceConnector Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Add RIO connector parameters.</summary>
        /// <param name="p_sLstSerializeConnectorParametersToAdd">Connector parameters Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        [Invoke(HasSideEffects = true)]
        public void AddParameterConnector(string p_sLstSerializeConnectorParametersToAdd, long p_lConnectorId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.AddParameterConnector (p_lConnectorId <" + p_lConnectorId + ">, p_sLstSerializeConnectorParametersToAdd <" + p_sLstSerializeConnectorParametersToAdd + ">)");

            //Deserialize List ConnectorParameters object.
            List<ConnectorParameters> p_oLstConnectorParameters = SerializationHelper.Deserialize<List<ConnectorParameters>>(p_sLstSerializeConnectorParametersToAdd); 

            try
            {
                ConnectorRepository.AddParameterConnector(p_oLstConnectorParameters, p_lConnectorId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.AddParameterConnector Exception [" + ex.Message + "]");
            }
        }

        #endregion Added

        #region Delete
        
        /// <summary>Delete RIO connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param>   
        [Invoke(HasSideEffects = true)]
        public void DeleteConnectorByConnectorId(long p_lConnectorId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.DeleteConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">");

            try
            {
                ConnectorRepository.DeleteConnectorByConnectorId(p_lConnectorId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.DeleteConnectorByConnectorId Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Delete List RIO connector.</summary>
        /// <param name="p_sConnectorListId">List Connector Id.</param>   
        [Invoke(HasSideEffects = true)]
        public void DeleteConnectorByListConnectorId(string p_sConnectorListId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.DeleteConnectorByListConnectorId (p_sConnectorListId <" + p_sConnectorListId + ">");

            try
            {
                ConnectorRepository.DeleteConnectorByListConnectorId(p_sConnectorListId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.DeleteConnectorByListConnectorId Exception [" + ex.Message + "]");
            }
        }

        #endregion Delete

        #region Update

        /// <summary>Update RIO connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param>   
        /// <param name="p_sConnectorDescription">Connector description.</param>
        /// <param name="p_sConnectorName">Connector name.</param>
        [Invoke(HasSideEffects = true)]
        public ReturnLong UpdateNameAndDescriptionConnectorByConnectorId(long p_lConnectorId, string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable declaration.
            var l_lConnectorId = new ReturnLong();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorDS.UpdateNameAndDescriptionConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">, p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");

            try
            {
                l_lConnectorId = ConnectorRepository.UpdateNameAndDescriptionConnectorByConnectorId(p_lConnectorId, p_sConnectorName, p_sConnectorDescription);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorDS.UpdateNameAndDescriptionConnectorByConnectorId Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_lConnectorId;
        }

        #endregion Update
    }
}